package com.guigu.controller.stock;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.stock.*;
import com.guigu.service.design.DFileService;
import com.guigu.service.stock.SCellService;
import com.guigu.service.stock.SGatherDetailsService;
import com.guigu.service.stock.SGatherService;
import com.guigu.service.stock.SThirdProductAreaService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 * 入库
 */
@Controller
@RequestMapping("/s-gather")
public class SGatherController {

    @Autowired
    private SGatherService sGatherService;

    @Autowired
    private SGatherDetailsService sGatherDetailsService;

    @Autowired
    private SCellService sCellService;

    @Autowired
    private DFileService dFileService;

    @Autowired
    private SThirdProductAreaService sThirdProductAreaService;

    //入库申请登记
    @GetMapping("/warehousing_Registration")
    public String warehousingRegistration(){
        return "jsp/stock/c/Warehousing_Application_Registration";
    }

    //入库申请审核
    @GetMapping("/warehousing_application_review")
    public String warehousingApplicationReview(){
        return "jsp/stock/c/Warehousing_Application_review";
    }

    //入库申请审核子页面
    @GetMapping("/Warehousing_Application_review_sub")
    public String goWarehouseOutApplicationReviewSub() {
        return "jsp/stock/c/Warehousing_Application_review_sub";
    }

    //入库登记预览
    @GetMapping("/Warehousing_Application_Registration_preview")
    public String warehouseoutApplicationRegistrationpreview(){
        return "jsp/stock/c/Warehousing_Application_Registration_preview";
    }

    //入库登记查询页面
    @GetMapping("/Inquiry_Warehousing_Application_requery")
    public String Inquiry_Warehousing_Application_requery(){
        return "jsp/stock/c/Inquiry_Warehousing_Application_requery";
    }

    //入库登记查询子面
    @GetMapping("/Inquiry_Warehousing_Application")
    public String Inquiry_Warehousing_Application(){
        return "jsp/stock/c/Inquiry_Warehousing_Application";
    }

    //制定入库单页面
    @GetMapping("/Formulat_Prepare_warehouse_entry")
    public String Formulat_Prepare_warehouse_entry(){
        return "jsp/stock/g/Formulat_Prepare_warehouse_entry";
    }

    //制定入库单子页面
    @GetMapping("/Prepare_warehouse_entry")
    public String Prepare_warehouse_entry(){
        return "jsp/stock/g/Prepare_warehouse_entry";
    }

    //入库登记
    @GetMapping("/Warehouse_Receiving_Registration")
    public String Warehouse_Receiving_Registration(){
        return "jsp/stock/h/Warehouse_Receiving_Registration";
    }

    //入库登记子页面
    @GetMapping("/Warehouse_Receving_Registration_sub")
    public String Warehouse_Receving_Registration_sub(){
        return "jsp/stock/h/Warehouse_Receving_Registration_sub";
    }

    //入库登记审核
    @GetMapping("/Warehouse_Receiving_Registration_review")
    public String Warehouse_Receiving_Registration_review(){
        return "jsp/stock/h/Warehouse_Receiving_Registration_review";
    }

    //入库登记审核子页面
    @GetMapping("/Warehouse_Receving_Registration_review_sub")
    public String Warehouse_Receving_Registration_review_sub(){
        return "jsp/stock/h/Warehouse_Receving_Registration_review_sub";
    }

    //添加出库表和出库明细表
    @PostMapping("/registration")
    @ResponseBody
    public Boolean registration(@RequestBody ArrayList<Object> data){
        System.out.println(data);
        //入库人
        String storer ="";
        //入库理由
        String reason ="";
        //总件数
        BigDecimal amountSum = BigDecimal.valueOf(0L);
        //总金额
        BigDecimal costPriceSum = BigDecimal.valueOf(0L);
        //登记时间
        String registerTime = "";
        //登记人
        String register = "";
        //备注
        String remark = "";
        //拿到第一个集合
        HashMap<String, Object> map = (HashMap<String, Object>) data.get(0);

        if(map.get("storer")!=null){
            storer = map.get("storer").toString();
        }
        if(map.get("reason")!=null){
            reason = map.get("reason").toString();
        }
        if(map.get("amountSum")!=null){
            amountSum = BigDecimal.valueOf(Long.parseLong(map.get("amountSum").toString()));
        }
        if(map.get("costPriceSum")!=null){
            costPriceSum =BigDecimal.valueOf(Long.parseLong(map.get("costPriceSum").toString()));
        }
        if(map.get("registerTime")!=null){
            registerTime = map.get("registerTime").toString();
        }
        if(map.get("register")!=null){
            register = map.get("register").toString();
        }
        if(map.get("remark")!=null){
            remark = map.get("remark").toString();
        }
        //获取出库明细表
        ArrayList<SGatherDetails> details = new ArrayList<>();
        for (int i = 1; i < data.size(); i++) {
            HashMap<String, Object> map1 = (HashMap<String, Object>) data.get(i);
            SGatherDetails sGatherDetails = new SGatherDetails();
            sGatherDetails.setParentId(null);
            sGatherDetails.setProductId(map1.get("productId").toString());
            sGatherDetails.setProductName(map1.get("productName").toString());
            sGatherDetails.setProductDescribe(map1.get("productDescribe").toString());
            sGatherDetails.setAmount(BigDecimal.valueOf(Long.parseLong(map1.get("num").toString())));
            sGatherDetails.setAmountUnit(map1.get("amountUnit").toString());
            sGatherDetails.setCostPrice(BigDecimal.valueOf(Long.parseLong(map1.get("costPrice").toString())));
            sGatherDetails.setSubtotal(BigDecimal.valueOf(Long.parseLong(map1.get("total").toString())));
            sGatherDetails.setGatheredAmount(BigDecimal.valueOf(Long.parseLong(map1.get("num").toString())));
            sGatherDetails.setGatherTag("0");
            details.add(sGatherDetails);
        }
        SGather sGather = new SGather();
        SGather endSGather = sGatherService.getEndSGather();
        CodeUtils codeUtils = new CodeUtils();
        String gaytherboundCode = codeUtils.getGatherboundCode(endSGather);

        sGather.setGatherId(gaytherboundCode);
        sGather.setStorer(storer);
        sGather.setReason(reason);
        sGather.setReasonexact(null);
        sGather.setAmountSum(amountSum);
        sGather.setCostPriceSum(costPriceSum);
        sGather.setGatheredAmountSum(BigDecimal.valueOf(0L));
        sGather.setRemark(remark);
        sGather.setRegister(register);
        sGather.setRegisterTime(new Date());
        sGather.setChecker(null);
        sGather.setCheckTime(null);
        sGather.setCheckTag("0");
        sGather.setAttemper(null);
        sGather.setAttemperTime(null);
        sGather.setGatherTag("1");
        sGather.setGatherCheckTag("0");

        int id = sGatherService.addSGather(sGather);
        boolean b=false;
        for(SGatherDetails sGatherDetails:details){
            sGatherDetails.setParentId(id);
            b = sGatherDetailsService.save(sGatherDetails);
        }
        return b;
    }

    //查询入库申请登记(未审核的入库申请)
    @PostMapping("/initialWarehousingTable")
    @ResponseBody
    public PageInfo<SGather> initialWarehousingTable(@RequestBody HashMap<String, Object> map) {

        PageInfo<SGather> sGatherPageInfo = sGatherService.selectSGatherByPage(map);
        return sGatherPageInfo;
    }

    //根据id查询入库申请表(未审核的入库申请)
    @GetMapping("/ItemApprovalTable")
    @ResponseBody
    public SGather ItemApprovalTable(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        QueryWrapper<SGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        SGather sGather = sGatherService.getOne(queryWrapper);
        return sGather;
    }

    //根据parentId查询出库明细表
    @GetMapping("/initialTemplate")
    @ResponseBody
    public ArrayList<SGatherDetails> initialTemplate(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        ArrayList<SGatherDetails> sGatherDetails = sGatherDetailsService.selectArrByParentId(id);
        return sGatherDetails;
    }

    @PostMapping("/initialAmount")
    @ResponseBody
    public HashMap<String, SCell> initialAmount(@RequestBody String productIdStr){
        SCell sCell = sCellService.selectProductId(productIdStr);
        HashMap<String, SCell> resultMap = new HashMap<>();
        resultMap.put("sCell", sCell);
        return resultMap;
    }

    //根据productId查询产品档案,在从产品档案获取三级分类Id查询库房表
    @GetMapping("/initialStoreroomTable")
    @ResponseBody
    public ArrayList<SThirdProductArea> initialStoreroomTable(HttpServletRequest req){
        String productId = req.getParameter("id");
        DFile dFile = dFileService.selectByProductId(productId);
        String thirdKindId = dFile.getThirdKindId();
        if(thirdKindId!=null){
            ArrayList<SThirdProductArea> sThirdProductAreas = sThirdProductAreaService.selectThird(thirdKindId);
            return sThirdProductAreas;
        }
        return null;
    }

    @PostMapping("/dispatch")
    @ResponseBody
    public Boolean dispatch(@RequestBody ArrayList<Object> data){
        System.out.println(data);
        //拿到第一个集合
        HashMap<String, Object> map = (HashMap<String, Object>) data.get(0);
        //入库编号
        Integer id =0;
        //入库详情理由
        String productDescribe="";
        //入库单编号
        String gatherId ="";
        //入库产品编号
        String productId ="";
        //入库数量
        String num ="";
        if(map.get("id")!=null){
            id =Integer.parseInt(map.get("id").toString());
        }
        if(map.get("productDescribe")!=null){
            productDescribe =map.get("productDescribe").toString();
        }
        if(map.get("gatherId")!=null){
            gatherId = map.get("gatherId").toString();
        }
        if(map.get("productId")!=null){
            productId = map.get("productId").toString();
        }
        if(map.get("num")!=null){
            num = map.get("num").toString();
        }
        //修改入库明细表
        SGatherDetails sGatherDetails = new SGatherDetails();
        sGatherDetails.setParentId(id);
        sGatherDetails.setProductId(productId);
        sGatherDetails.setGatherTag("2");
        sGatherDetails.setProductDescribe(productDescribe);
        boolean b = sGatherDetailsService.updateSGatherDetails(sGatherDetails);
        boolean flag = sGatherDetailsService.selectParentId(id, "0");

        if(!flag){
            //修改入库表
            SGather sGather = new SGather();
            sGather.setGatherId(gatherId);
            sGather.setGatherTag("2");//已调度
            boolean b2 = sGatherService.updateSGather(sGather);
        }
        return b;
    }

    //入库登记
    @PostMapping("/recevingRegistration")
    @ResponseBody
    public Boolean recevRegistration(@RequestBody ArrayList<Object> data) {
        //拿到第一个集合
        HashMap<String, Object> map = (HashMap<String, Object>) data.get(0);
        //入库单编号
        String gatherId ="";
        //确认入库总件数
        BigDecimal gatheredAmountSum=BigDecimal.valueOf(0L);

        if(map.get("gatherId")!=null){
            gatherId = map.get("gatherId").toString();
        }
        if(map.get("gatheredAmountSum")!=null){
            gatheredAmountSum = BigDecimal.valueOf(Long.valueOf(map.get("gatheredAmountSum").toString()));
        }
        SGather sGather = new SGather();
        sGather.setGatherId(gatherId);
        sGather.setGatherTag("3");
        sGather.setGatheredAmountSum(gatheredAmountSum);
        sGather.setGatherCheckTag("1");
        sGatherService.updateSGather(sGather);
        //根据入库单编号查询入库表主键
        int id = sGatherService.findBySGatherId(gatherId);
        //获取入库明细表
        ArrayList<SGatherDetails> details = new ArrayList<>();
        for(int i=1;i<data.size();i++){
            HashMap<String, Object> map1 = (HashMap<String, Object>) data.get(i);
            SGatherDetails sGatherDetails = new SGatherDetails();
            sGatherDetails.setParentId(id);
            sGatherDetails.setProductId(map1.get("productId").toString());
            sGatherDetails.setGatheredAmount(BigDecimal.valueOf(Long.valueOf(map1.get("gatheredAmount").toString())));
            details.add(sGatherDetails);
        }
        boolean b = false;
        for(SGatherDetails sGatherDetails:details){
            b = sGatherDetailsService.updateSGatherDetails(sGatherDetails);
        }
        return  b;
    }

    //入库登记审核
    @PostMapping("/recevingRecheck")
    @ResponseBody
    public Boolean OutboundRecheck(@RequestBody ArrayList<Object> data){
        //拿到第一个集合
        HashMap<String, Object> map = (HashMap<String, Object>) data.get(0);
        //出库单编号
        String gatherId ="";
        BigDecimal gatheredAmountSum=BigDecimal.valueOf(0L);
        if(map.get("gatherId")!=null){
            gatherId = map.get("gatherId").toString();
        }
        if(map.get("gatherId")!=null){
            gatheredAmountSum = BigDecimal.valueOf(Long.valueOf(map.get("gatheredAmountSum").toString()));
        }

        SGather sGather = new SGather();
        sGather.setGatherId(gatherId);
        sGather.setGatherCheckTag("2");
        sGatherService.updateSGather(sGather);
        //根据入库单编号查询入库表主键
        int id = sGatherService.findBySGatherId(gatherId);
        //获取入库明细表
        ArrayList<SGatherDetails> details = new ArrayList<>();
        for(int i=1;i<data.size();i++){
            HashMap<String, Object> map1 = (HashMap<String, Object>) data.get(i);
            SGatherDetails sGatherDetails = new SGatherDetails();
            sGatherDetails.setParentId(id);
            sGatherDetails.setProductId(map1.get("productId").toString());
            sGatherDetails.setGatheredAmount(BigDecimal.valueOf(Long.valueOf(map1.get("gatheredAmount").toString())));
            sGatherDetails.setGatherTag("3");
            details.add(sGatherDetails);
        }
        boolean b=false;

        for(SGatherDetails sGatherDetails:details){
            b = sGatherDetailsService.updateSGatherDetails(sGatherDetails);
            //获取入库明细表的产品编号,根据产品编号查询scell表，修改scell表中的总件数
            String productId = sGatherDetails.getProductId();
            SCell sCell = sCellService.selectProductId(productId);
            BigDecimal amount = sCell.getAmount();
            Long amountnum=amount.longValue();
            Long gatheredAmount =sGatherDetails.getGatheredAmount().longValue();
            amount=BigDecimal.valueOf(amountnum+gatheredAmount);
            sCell.setProductId(productId);
            sCell.setAmount(amount);
            boolean b1 = sCellService.updateProductId(sCell);
            System.out.println(b1);
        }
        return  b;
    }
}

